<?php

namespace teach\service;

use backend\models\MallVideo;
use Yii;

class Video
{
    /**
     * 查询视频
     * @param $condition
     * @param $page
     * @param int $pre_page
     * @return array
     */
    public function videoList($condition, $page, $pre_page = 10)
    {
        $count = $this->videoCount($condition);
        $data = ['total' => 0, 'total_page' => 0, 'page' => 1, 'pre_page' => $pre_page, 'lists' => []];

        if ($count) {
            $total_page = ceil( $count / $pre_page);

            $sql = "SELECT * FROM mall_video WHERE " . $condition . " ORDER BY sort DESC, create_time DESC " . " LIMIT " . ($page - 1) * $pre_page . " , " . $pre_page;
            $res = Yii::$app->db->createCommand($sql)->queryAll();

            $data = ['total' => $count, 'total_page' => $total_page, 'page' => $page, 'pre_page' => $pre_page, 'lists' => $res];
        }

        return $data;

    }

    /**
     * 统计视频
     * @param $condition
     * @return false|null|string
     */
    public function videoCount($condition)
    {
        $sql = "SELECT COUNT(*) FROM mall_video WHERE " . $condition;
        return Yii::$app->db->createCommand($sql)->queryScalar();
    }

    /**
     * 点击播放数加一
     * @param $condition
     */
    public function videoIncrease($condition)
    {
        $sql = "UPDATE mall_video SET play_count = play_count + 1 WHERE " . $condition;
        Yii::$app->db->createCommand($sql)->execute();
    }
}